import datetime

...
...
import random
from ICBC_bank import *


# 1.准备一个数据库 和 银行名称
bank = {}  # 空的数据库
bank_name = "中国工商银行昌平回龙观支行"  # 银行名称写死


# 2.入口程序
def welcom():
    print("*********************************")
    print("*     中国工商银行昌平回龙观支行     *")
    print("*********************************")
    print("* 1.开户                         *")
    print("* 2.存钱                         *")
    print("* 3.取钱                         *")
    print("* 4.转账                         *")
    print("* 5.查询                         *")
    print("* 6.退出                         *")
    print("*********************************")


# 银行开户逻辑
def bank_addUser(accout, username, password, country, province, street, gate, money):
    # 1.判断数据库是否已满
    key = "select username from user "
    key1 = "select username from user where username = %s"
    para = username
    if len(select(key,param=None,mode="all")) >= 100:
        return 3
    # 2.判断用户是否存在
    if select(key1,para,mode="one") == ():
        return 2
    # 正常开户
    sql = "insert into user value (%s,%s,%s,%s,%s,%s,%s,%s,NOW(),%s)"
    param = [accout, username, password, country, province, street, gate, money,bank_name]
    insert(sql, param)
    # bank[username] = {
    #     "account": accout,
    #     "password": password,
    #     "country": country,
    #     "province": province,
    #     "street": street,
    #     "gate": gate,
    #     "money": money,
    #     "bank_name": bank_name
    # }1
    return 1


# 用户开户的操作逻辑
def addUser():
    username = input("请输入您的用户名 :")
    password = input("请输入您的开户密码 :")
    country = input("请输入您的国籍 :")
    province = input("请输入您的居住省份 :")
    street = input("请输入您的街道 :")
    gate = input("请输入您的门牌号 :")
    money = input("请输入您的开户初始余额 :")
    # 随机产生8位数字
    account = random.randint(10000000, 99999999)

    status = bank_addUser(account, username, password, country, province, street, gate, money)
    if status == 3:
        print("对不起，用户库已满，请携带证件到其他银行办理！")
    elif status == 2:
        print("对不起，该用户已存在！请勿重复开户！")
    elif status == 1:
        print("开户成功！以下是您的个人开户信息 :")
        info = '''
            -------------个人信息-------------
            用户名: %s
            密码: %s
            账号: %s
            地址信息
                国家: %s
                省份: %s
                街道: %s
                门牌号: %s
            余额: %s
            开户银行: %s
            ---------------------------------
        '''
        print(info % (username, password, account, country, province, street, gate, money, bank_name))


def addMoney():
    num = input("请输入您要存款的用户 :")
    num1 = input("请输入您的密码 :")
    sql = "select username,password from user where username = %s and password = %s"
    param = [num,num1]
    if select(sql,param,mode="one") != ():
        write = input("请输入您要存储的金额 :")
        sql1 = "select * from user where username = %s "
        b = int(select(sql1,num,mode="one")[7])
        b +=  int(write)
        sql = "update user set money = %s where username = %s"
        p = [b,num]
        Change(sql,p)
        print("您当前余额为 :{}元".format(b))
    else:
        print("用户密码不正确！")


def getMoney():
    num = input("请输入您要取款的用户 :")
    num1 = input("请输入您的密码 :")
    sql = "select username,password from user where username = %s and password = %s"
    param = [num,num1]
    if select(sql,param,mode="one") != ():
        write = input("请输入您要取储的金额 :")
        sql1 = "select * from user where username = %s "
        b = int(select(sql1,num,mode="one")[7])
        b -= int(write)
        if b < 0:
            print("余额不足！")
        else:
            sql = "update user set money = %s where username = %s"
            p = [b, num]
            Change(sql, p)
            print("您当前余额为 :{}元".format(b))
    else:
        print("用户密码不正确！")


def chageMoney():
    num = input("请输入用户名 :")
    num1 = input("请输入密码 :")
    sql = "select username,password from user where username = %s and password = %s"
    param = [num, num1]
    if select(sql, param, mode="one") != ():
        num3 = input("请输入您要转账的用户 :")
        key1 = "select username from user where username = %s"
        if select(key1, num3, mode="one")[0] == num3:
            num4 = input("请输入您要转账的金额 :")
            if num4.isdigit():
                sql1 = "select * from user where username = %s "
                b = int(select(sql1, num, mode="one")[7])
                b -= int(num4)
                if b < 0:
                    print("余额不足！")
                else:
                    sql = "update user set money = %s where username = %s"
                    p = [b, num]
                    Change(sql, p)

                    sql1 = "select * from user where username = %s "
                    b1 = int(select(sql1, num3, mode="one")[7])
                    b1 += int(num4)
                    sql2 = "update user set money = %s where username = %s"
                    p1 = [b1,num3]
                    Change(sql2,p1)
                    print("转账成功！")
            else:
                print("您输入的不正确！")
        else:
            print("您输入的用户不存在！")
    else:
        print("您输入账户密码不正确！")


def select_user():
    num = input("请输入您要查询的用户 :")
    num1 = input("请输入您的密码 :")
    sql = "select username,password from user where username = %s and password = %s"
    param = [num, num1]
    if select(sql, param, mode="one") != ():
        sql1 = "select * from user where username = %s "
        b = select(sql1, num, mode="one")
        info = '''
                    -------------个人信息-------------
                    用户名: %s
                    密码: %s
                    账号: %s
                    地址信息
                        国家: %s
                        省份: %s
                        街道: %s
                        门牌号: %s
                    余额: %s
                    开户银行: %s
                    ---------------------------------
                '''
        print(info % (num, num1, b[0], b[3], b[4],b[5], b[6], b[7], b[9]))
    else:
        print("用户不存在，请重新输入！")


while True:
    welcom()
    chose = input("请输入您的业务 :")
    if chose == "1":
        addUser()
    elif chose == "2":
        addMoney()
    elif chose == "3":
        getMoney()
    elif chose == "4":
        chageMoney()
    elif chose == "5":
        select_user()
    elif chose == "6":
        print("欢迎下次光临！")
        break
    else:
        print("输入有误请重新输入")
